package com.lh.java.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 ************************************************ 
 ** Module : Mysql 原始的JDBC方式数据库联结 Date: 2012-9-5 Time: 下午03:04:44 Author lh
 ************************************************ 
 */
public class JDBCOriginalConnect {
	// 驱动程序名

	static String driver = "com.mysql.jdbc.Driver";

	// URL指向要访问的数据库名myad

	static String url = "jdbc:mysql://127.0.0.1:3306/myad";

	// MySQL配置时的用户名

	static String user = "root";

	// Java连接MySQL配置时的密码

	static String password = "root";

	public static void main(String[] args) {
		// OriginalConnect or =new OriginalConnect();
		try {
			// 加载驱动程序
			Class.forName(driver);
			// 连续数据库
			Connection conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed()){
				System.out.println("Succeeded connecting to the Database!");
			}				
			// statement用来执行SQL语句
			Statement statement = conn.createStatement();
			// 要执行的SQL语句
			String sql1 = "select * from class_student_id";
			ResultSet rs = statement.executeQuery(sql1);			
			
			
			System.out.println("-----------------");
			System.out.println("执行结果如下所示:");
			System.out.println("-----------------");
			System.out.println(" 学号" + "\t" + " 姓名");
			System.out.println("-----------------");
			//String name = null;
			while (rs.next()) {				
				System.out.println(rs.getString("student_id") + "\t" + rs.getString("name"));
			}
			//rs.close();
			String sq2 = "select * from class_student_id";
			System.out.println("======= other sql ===========");
			rs = statement.executeQuery(sq2);
			while (rs.next()) {				
				//System.out.println(rs.getString("name")+"\t" + rs.getInt("age"));
				System.out.println( rs.getInt("age"));
			}
			rs.close();
			statement.close();
			conn.close();
			
		} catch (ClassNotFoundException e1) {
			System.out.println("Sorry,can`t find the Driver!");
			e1.printStackTrace();
		} catch (SQLException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		finally{
			
		}

	}

}
